//
//  ZhuceViewController.m
//  DimComDemo
//
//  Created by 我 on 2019/1/23.
//  Copyright © 2019年 于跃隆. All rights reserved.
//

#import "ZhuceViewController.h"
#import <SMS_SDK/SMSSDK.h>
#import "MainViewController.h"
#import "AFNetworking.h"
#import "ViewController.h"
@interface ZhuceViewController ()<UITextFieldDelegate>

{
   
    
    UITextField *yanzhengma;
    
    UIButton *fsbtn;
    
    UIButton *zhuce;
    
    
    
}
@property (nonatomic ,weak) UITextField *mima;
@property(nonatomic,strong)UITextField *shoujihao;
@property(nonatomic,strong)NSMutableArray *datascoure;
@end

@implementation ZhuceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   UIImageView *beijng=[[UIImageView alloc]initWithFrame:self.view.frame];
    beijng.image=[UIImage imageNamed:@"1"];
    [self.view addSubview:beijng];
    
    //手机图标
    UIImageView *iphone=[[UIImageView alloc]initWithFrame:CGRectMake(40, 200, 20, 30)];
    iphone.image=[UIImage imageNamed:@"2"];
    [self.view addSubview:iphone];
    //验证码d图标
    UIImageView *anquan=[[UIImageView alloc]initWithFrame:CGRectMake(40, 260, 25, 30)];
    anquan.image=[UIImage imageNamed:@"login_icon_Verification code_n_hdpi"];
    [self.view addSubview:anquan];
    //密码图标
    UIImageView *ma=[[UIImageView alloc]initWithFrame:CGRectMake(40, 320, 25, 30)];
    ma.image=[UIImage imageNamed:@"3"];
    [self.view addSubview:ma];
    
    //手机号的下划线
    UIView *xiahuaxian1=[[UIView alloc]initWithFrame:CGRectMake(35, 245, 330, 2)];
    xiahuaxian1.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:xiahuaxian1];
    
    //验证码的下划线
    UIView *xiahuaxian2=[[UIView alloc]initWithFrame:CGRectMake(35, 305, 330, 2)];
    xiahuaxian2.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:xiahuaxian2];
    //密码的下划线
    UIView *xiahuaxian=[[UIView alloc]initWithFrame:CGRectMake(35, 365, 330, 2)];
    xiahuaxian.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:xiahuaxian];
    
    
    //手机号输入框
    self.shoujihao=[[UITextField alloc]initWithFrame:CGRectMake(80, 200, 240, 40)];
    self.shoujihao.placeholder=@"手机号";
    self.shoujihao.font=[UIFont systemFontOfSize:20];
    self.shoujihao.textColor=[UIColor whiteColor];
    [self.view addSubview:self.shoujihao];
    //验证码输入框
    yanzhengma=[[UITextField alloc]initWithFrame:CGRectMake(80, 260, 240, 40)];
    yanzhengma.placeholder=@"验证码";
    yanzhengma.font=[UIFont systemFontOfSize:20];
    yanzhengma.textColor=[UIColor whiteColor];
    [self.view addSubview:yanzhengma];
    
    //暗文变明为的按钮
    UIButton *kan = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [kan setImage:[UIImage imageNamed:@"ico_mimabukejian"] forState:UIControlStateNormal];
    [kan setImage:[UIImage imageNamed:@"ico_mimakejian"] forState:UIControlStateHighlighted];
    //点击方法
    [kan addTarget:self action:@selector(textFieldRightBtnTouchDown:) forControlEvents:UIControlEventTouchDown];
    [kan addTarget:self action:@selector(textFieldRightBtnTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    
    //密码输入框
    UITextField *mima = [[UITextField alloc] initWithFrame:CGRectMake(80, 320,290, 44)];
    mima.placeholder=@"请输入密码";
     mima.delegate = self;
    mima.font = [UIFont systemFontOfSize:20];
    mima.secureTextEntry = YES;
    mima.rightView = kan;
    mima.rightViewMode = UITextFieldViewModeAlways;
    mima.delegate = self;
    [self.view addSubview:mima];
    self.mima = mima;
    //发送验证码
    fsbtn=[[UIButton alloc]initWithFrame:CGRectMake(205, 260, 160, 40)];
//    fsbtn.layer.borderWidth=0.1;
//    fsbtn.layer.backgroundColor=[UIColor whiteColor].CGColor;
    //fsbtn.backgroundColor=[UIColor whiteColor];
    [fsbtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [fsbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [fsbtn addTarget:self action:@selector(fasong) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:fsbtn];
    
    //注册按钮
    zhuce=[[UIButton alloc]initWithFrame:CGRectMake(70, 550, 280, 40)];
    zhuce.backgroundColor=[UIColor whiteColor];
    zhuce.layer.cornerRadius=8;
    zhuce.layer.masksToBounds=YES;
    [zhuce setTitle:@"注册" forState:UIControlStateNormal];
    [zhuce setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [zhuce addTarget:self action:@selector(zhuce) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:zhuce];
    
    UIButton *youzhanghao=[[UIButton alloc]initWithFrame:CGRectMake(240, 400, 150, 40)];
    [youzhanghao setTitle:@"已有账号登录" forState:UIControlStateNormal];
    [youzhanghao addTarget:self action:@selector(youzhanghao) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:youzhanghao];
    
   
    
}
-(void)fasong{
    
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.shoujihao.text zone:@"86"  result:^(NSError *error) {
        __block NSInteger timeOut = 59;
        //执行队列
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        //计时器 -》dispatch_source_set_timer自动生成
        dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
        dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
        dispatch_source_set_event_handler(timer, ^{
            if (timeOut <= 0) {
                dispatch_source_cancel(timer);
                //主线程设置按钮样式-》
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self->fsbtn setTitle:@"发送验证码" forState:UIControlStateNormal];
                    [self->fsbtn setUserInteractionEnabled:YES];
                });
            }else{
                //开始计时
                //剩余秒数 seconds
                NSInteger seconds = timeOut % 60;
                NSString *strTime = [NSString stringWithFormat:@"（%.1ld）秒重新获取",seconds];
                //主线程设置按钮样式
                dispatch_async(dispatch_get_main_queue(), ^{
                    [UIView beginAnimations:nil context:nil];
                    [UIView setAnimationDuration:1.0];
                    [
                     self->fsbtn setTitle:[NSString stringWithFormat:@"%@",strTime] forState:UIControlStateNormal];
                    [UIView commitAnimations];
                    //计时器件不允许点击
                    [self->fsbtn setUserInteractionEnabled:NO];
                });
                timeOut--;
            }
        });
        dispatch_resume(timer);
        
        if (!error)
        {
            NSLog(@"发送成功");
        }
        else
        {
            NSLog(@"发送失败:///////%@",error);
            
        }
    }];

    
    
    
}
-(void)zhuce{
    
    [SMSSDK commitVerificationCode:yanzhengma.text phoneNumber:self.shoujihao.text zone:@"86" result:^(NSError *error) {
        
        
        
        AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"application/json"];
        NSString *str=self.shoujihao.text;
        NSString *strr=self.mima.text;
        NSDictionary *dic=@{@"phone":str,@"pwd":strr};
        [manager POST:@"http://172.17.8.100/small/user/v1/register" parameters:dic headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            self.datascoure = responseObject;
            NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"message"]];
            NSLog(@"--aasdasd%@",str);
            //UIAlertView
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:str delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
            [alert show];
            NSLog(@"数据有,%@",self.datascoure);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
        }
         ];
        
        if (!error)
        {
            NSLog(@"验证成功");
            
            
            MainViewController *new=[[MainViewController alloc]init];
            [self presentViewController:new animated:YES completion:nil];
            
          
            
        }
        else
        {
            NSLog(@"验证失败:///////%@",error);
            //NSLog(@"手机验证码为空%@",error);
        }
    }];
    
}
-(void)youzhanghao{
    ViewController *view=[ViewController new];
    [self presentViewController:view animated:YES completion:nil];
    
}
- (void)textFieldRightBtnTouchDown:(UIButton *)rightBtn {
    self.mima.secureTextEntry = NO;
    [self.mima becomeFirstResponder];
    
    self.mima.font = nil;
    self.mima.font = [UIFont systemFontOfSize:20];
    self.mima.textColor=[UIColor whiteColor];
}

- (void)textFieldRightBtnTouchUpInside:(UITextField *)textField {
    self.mima.secureTextEntry = YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.mima endEditing:YES];
}
- (BOOL)textField:(UITextField *)mima shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *allStr = [mima.text stringByReplacingCharactersInRange:range withString:string];
    
    if (mima.isSecureTextEntry == YES) {
        mima.text = allStr;
        return NO;
    }
    
    return YES;
}

@end
